/*
 * @Author: C.
 * @Date: 2025-05-08 18:09:20
 */
import type {
  AccessModeType,
  ComponentRecordType,
  GenerateMenuAndRoutesOptions,
} from '@cwind/types';

import { generateAccessible } from '@cwind/access';

import { message } from 'ant-design-vue';

import { getAllMenusApi } from '#/api';
import { BasicLayout, IFrameView } from '#/layouts';
import { $t } from '#/locales';

import { overridesPreferences } from '../preferences';

const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');

async function generateAccess(options: GenerateMenuAndRoutesOptions) {
  const pageMap: ComponentRecordType = import.meta.glob('../views/**/*.vue');

  const layoutMap: ComponentRecordType = {
    BasicLayout,
    IFrameView,
  };
  return await generateAccessible(
    overridesPreferences?.app?.accessMode as AccessModeType,
    {
      ...options,
      fetchMenuListAsync: async () => {
        message.loading({
          content: `${$t('common.loadingMenu')}...`,
          duration: 1.5,
        });
        return await getAllMenusApi();
      },
      // 可以指定没有权限跳转403页面
      forbiddenComponent,
      // 如果 route.meta.menuVisibleWithForbidden = true
      layoutMap,
      pageMap,
    },
  );
}

export { generateAccess };
